<template>
  <div ref="scrollCon" @DOMMouseScroll="handlescroll" @mousewheel="handlescroll" class="tags-outer-scroll-con">
    <div class="close-all-tag-con">
      <Dropdown transfer @on-click="handleTagsOption">
        <Icon type="ios-arrow-down" size="16" />
        <DropdownMenu slot="list">
          <DropdownItem name="clearAll">
            <Icon type="ios-close-circle" />
            <span style="padding-left: 10px">关闭所有</span>
          </DropdownItem>
          <DropdownItem name="clearOthers">
            <Icon type="ios-close-circle" />
            <span style="padding-left: 10px">关闭其他</span>
          </DropdownItem>
        </DropdownMenu>
      </Dropdown>
    </div>
    <div ref="scrollBody" class="tags-inner-scroll-body" :style="{ left: tagBodyLeft + 'px' }">
      <transition-group name="taglist-moving-animation">
        <Tag ref="tagsPageOpened" type="dot" v-for="item in pageList" :key="`${item.path}-page`" :name="item.path" :closable="item.path != '/home'" :color="$route.path == item.path?'primary':'default'" @on-close="closePage" @click.native="linkTo(item.path)">{{item.title}}</Tag>
      </transition-group>
    </div>
  </div>
</template>
<script>
const OFFSET_WIDTH = 80;
export default {
  name: "tags",
  data() {
    return {
      tagBodyLeft: 0,
      refsTag: [],
    };
  },
  props: ["pageList"],
  mounted() {
    this.refsTag = this.$refs.tagsPageOpened;
  },
  watch: {
    $route(to) {
      this.$nextTick(() => {
        this.refsTag.forEach((item, index) => {
          if (to.path === item.path) {
            let tag = this.refsTag[index].$el;
            this.moveToView(tag);
          }
        });
      });
    }
  },
  methods: {
    handleTagsOption(type) {
      if (type === "clearAll") {
        this.$store.dispatch('other/clearAllTags');
        this.$router.push({
          path: "/"
        });
        this.tagBodyLeft = 0;
      } else if (type === "clearOthers") {
        this.$store.dispatch('other/clearOtherTags', this);
        this.tagBodyLeft = 0;
      }
    },
    closePage(event, path) {
      this.$store.dispatch('other/removePageList', {
        that: this,
        path
      })
    },
    linkTo(path) {
      this.$router.push(path)
    },
    moveToView(tag) {
      if (tag.offsetLeft < -this.tagBodyLeft) {
        // 标签在可视区域左侧
        this.tagBodyLeft = -tag.offsetLeft + 10;
      } else if (
        tag.offsetLeft + 10 > -this.tagBodyLeft &&
        tag.offsetLeft + tag.offsetWidth <
          -this.tagBodyLeft + this.$refs.scrollCon.offsetWidth - OFFSET_WIDTH
      ) {
        // 标签在可视区域
        this.tagBodyLeft = Math.min(
          0,
          this.$refs.scrollCon.offsetWidth -
            OFFSET_WIDTH -
            tag.offsetWidth -
            tag.offsetLeft -
            20
        );
      } else {
        // 标签在可视区域右侧
        this.tagBodyLeft = -(
          tag.offsetLeft -
          (this.$refs.scrollCon.offsetWidth - OFFSET_WIDTH - tag.offsetWidth) +
          20
        );
      }
    },
    handlescroll(e) {
      var type = e.type;
      let delta = 0;
      if (type === "DOMMouseScroll" || type === "mousewheel") {
        delta = e.wheelDelta ? e.wheelDelta : -(e.detail || 0) * 40;
      }
      let left = 0;
      if (delta > 0) {
        left = Math.min(0, this.tagBodyLeft + delta);
      } else {
        if (
          this.$refs.scrollCon.offsetWidth - OFFSET_WIDTH <
          this.$refs.scrollBody.offsetWidth
        ) {
          if (
            this.tagBodyLeft <
            -(
              this.$refs.scrollBody.offsetWidth -
              this.$refs.scrollCon.offsetWidth +
              OFFSET_WIDTH
            )
          ) {
            left = this.tagBodyLeft;
          } else {
            left = Math.max(
              this.tagBodyLeft + delta,
              this.$refs.scrollCon.offsetWidth -
                this.$refs.scrollBody.offsetWidth -
                OFFSET_WIDTH
            );
          }
        } else {
          this.tagBodyLeft = 0;
        }
      }
      this.tagBodyLeft = left;
    },
  }
};
</script>
<style lang="less" scoped>
.tags-outer-scroll-con {
  position: relative;
  box-sizing: border-box;
  padding-right: 120px;
  width: 100%;
  height: 40px;
  .ivu-tag {
    cursor: pointer;
    line-height: 30px;
  }
  .tags-inner-scroll-body {
    position: absolute;
    padding: 2px 10px;
    overflow: visible;
    white-space: nowrap;
    transition: left 0.3s ease;
  }
  .close-all-tag-con {
    position: absolute;
    right: 0;
    top: 0;
    box-sizing: border-box;
    padding: 5px;
    margin: 5px 0px;
    text-align: center;
    width: 40px;
    height: 30px;
    background: white;
    cursor: pointer;
    border-radius: 4px;
    box-shadow: -3px 0 15px 3px rgba(0, 0, 0, 0.1);
    z-index: 10;
  }
}
/deep/ .ivu-tag-dot-inner{
  transition: .2s;
}
</style>
